#include <iostream>
#include <algorithm>

using namespace std;

long long temp[32][32] = {};
long long e[32][32] = {};
long long w[32] = {};
int pi[32][32] = {};

void dfs(int start, int index)
{
    if (temp[start][index])
    {
        return;
    }
    
    int mx = 0;
    for (int i = 0; i < 32; i++)
    {
        if (e[index][i])
        {
            dfs(start, i);
            if (temp[start][i] > temp[start][mx])
            {
                mx = i;
            }
        }
    }

    temp[start][index] = temp[start][mx] + w[index];
    pi[start][index] = mx;
}

int main(int argc, char const *argv[])
{
    int n, p;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> w[i];
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = i + 1; j <= n; j++)
        {
            cin >> e[i][j];
        }
    }
    int mxs = 0;
    for (int i = 1; i <= n; i++)
    {
        dfs(i, i);
        if (temp[i][i] > temp[mxs][mxs])
        {
            mxs = i;
        }
    }
    
    p = mxs;
    while (p)
    {
        cout << p << " ";
        p = pi[mxs][p];
    }
    
    cout << endl << temp[mxs][mxs];
    return 0;
}
